package com.haole.ecommercepay.profitsharing.service.impl;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haole.ecommercepay.common.HttpUtils;
import com.haole.ecommercepay.profitsharing.entity.AddReceiverParam;
import com.haole.ecommercepay.profitsharing.entity.QueryOrderParam;
import com.haole.ecommercepay.profitsharing.entity.ReturnOrdersParam;
import com.haole.ecommercepay.profitsharing.entity.orders.OrdersParam;
import com.haole.ecommercepay.profitsharing.service.ProfitsharingService;
import com.haole.ecommercepay.profitsharing.url.ProfitsharingUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author haole
 */
@Service("profitsharingService")
public class ProfitsharingServiceImpl implements ProfitsharingService {

    private static final Logger logger = LoggerFactory.getLogger(ProfitsharingServiceImpl.class);

    /**
     * 请求分账
     * @param param
     * @return
     */
    @Override
    public JSONObject orders(OrdersParam param) {
        String requestParam = JSON.toJSONString(param);
        logger.info("orders param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", ProfitsharingUrl.ORDERS,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("orders result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 查询分账结果
     * @param param
     * @return
     */
    @Override
    public JSONObject queryOrder(QueryOrderParam param) {
        logger.info("queryOrder param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(ProfitsharingUrl.QUERY_ORDER,param.getSub_mchid(),param.getTransaction_id(),param.getOut_order_no()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("queryOrder result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 请求分账回退
     * @param param
     * @return
     */
    @Override
    public JSONObject returnOrders(ReturnOrdersParam param) {
        String requestParam = JSON.toJSONString(param);
        logger.info("returnorders param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", ProfitsharingUrl.RETURNORDERS,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("returnorders result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 添加分账接收方
     * @param param
     * @return
     */
    @Override
    public JSONObject addReceiver(AddReceiverParam param) {
        String requestParam = JSON.toJSONString(param);
        logger.info("addReceiver param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", ProfitsharingUrl.ADD_RECEIVER,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("addReceiver result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 删除分账接收方
     * @param param
     * @return
     */
    @Override
    public JSONObject deleteReceiver(AddReceiverParam param) {
        String requestParam = JSON.toJSONString(param);
        logger.info("deleteReceiver param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", ProfitsharingUrl.DELETE_RECEIVER,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("deleteReceiver result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
}
